export function connectWebSocket(url, messageHandler) {
    const ws = new WebSocket(url)

    ws.onopen = () => {
        console.log('WebSocket connected')
    }

    ws.onmessage = (event) => {
        try {
            const data = JSON.parse(event.data)
            messageHandler(data)
        } catch (error) {
            console.error('Error parsing WebSocket message:', error)
        }
    }

    ws.onerror = (error) => {
        console.error('WebSocket error:', error)
    }

    ws.onclose = () => {
        console.log('WebSocket disconnected')
    }

    return ws
}